#include "head.h"



int do_login(int sockfd, MSG *pbuf)
{	
    pbuf->type = LOGIN;

    puts("please input your name");
    gets(pbuf->name);
	puts("please input your passwd");
    gets(pbuf->passwd);
    send(sockfd, pbuf, sizeof(pbuf) , 0);
    recv(sockfd, pbuf, sizeof(pbuf) , 0);
    if(pbuf->ret == STAFF && strcmp(pbuf->data,"ok") == 0 )
        pbuf->ret = STAFF;
    else if(pbuf->ret == ADMIN && strcmp(pbuf->data,"ok") == 0)
        pbuf->ret = ADMIN;

    return pbuf->ret;
	
	
	
}
int do_change_passwd(int sockfd, MSG *pbuf)
{
    pbuf->type = STAFF_CHANGE_PASSWD;
    puts("please input your new passwd");
    gets(pbuf->passwd);
    send(sockfd, pbuf, sizeof(pbuf) , 0);
    recv(sockfd, pbuf, sizeof(pbuf) , 0);
    if(strcmp(pbuf->data,"ok") == 0)
    printf("change passwd success\n");

    return 0;

}
int do_search_user_information(int sockfd, MSG *pbuf)
{


}
void do_stuff(int sockfd, MSG *pbuf)
{
        int input;
        char cleanbuf[64];

        while (1) {
            printf("***********************************************\n");
            printf("* 1: change_passwd   2: quit   3:search_user_information*\n");
            printf("***********************************************\n");
            printf("please choose : ");
            //获取用户输入
            if(scanf("%d", &input) == 0){
                fgets(cleanbuf, 64, stdin);  //类型错误需要重新清除输入缓冲区
                continue;
            }

            switch (input) {
            case 1:  //查询单词
                printf("\n");
                do_change_passwd(sockfd, pbuf);
                printf("\n");
                break;
            case 2:  //返回上一级
                return;
            case 3:
                printf("\n");
                do_search_user_information(sockfd, pbuf);
                printf("\n");
            default:
                break;
            }
        }
}


int do_admin(int sockfd, MSG *pbuf)
{
    
}

int main(int argc, char *argv[])
{
    int sockfd, ret;
    struct sockaddr_in servaddr;
    MSG buf;
    char clean[64];

    if (argc < 3)
    {
        printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
        exit(-1);
    }

    //创建客户端socket
    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("fail to socket");
        exit(-1);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = PF_INET;
    servaddr.sin_port = htons(atoi(argv[2]));
    servaddr.sin_addr.s_addr = inet_addr(argv[1]);

    //连接服务器
    if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("fail to connect");
        exit(-1);
    }

    ret = do_login(sockfd, &buf);
    if(ret == STAFF){  //员工
        puts("stuff login");
        do_stuff(sockfd, &buf);
    }else{  //管理员
        puts("admin login");
        do_admin(sockfd, &buf);
    }
}
